﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Net;
using System.Web;
using System.Diagnostics;

namespace TridentReport
{
    public partial class grid : Form
    {
        
        WebBrowser gridpage = new WebBrowser();        
        DataTable tridenttable;
        DataRow trow;
        HtmlElementCollection rows;        
        HtmlElement bprow;
        HtmlElement bpstoreseq;        
        HtmlElement row;
        HtmlElement storeseq;
        HtmlElementCollection cells;
        HtmlElement cell;
        int rowcount;        
        string bpstrseq;         
        int tr;
        string str;
        string seq;
        string tstrseq;
        string url;        

        public grid(DataTable trident, int i)
        {
            InitializeComponent();
            tr = i;
            tridenttable = trident;
            trow = tridenttable.Rows[tr];
            str = Convert.ToString(trow["str_num"]);
            seq = Convert.ToString(trow["str_seq"]);
            if (seq == "0" || seq == "1" || seq == "2" || seq == "3" || seq == "4" || seq == "5" ||
                seq == "6" || seq == "7" || seq == "8" || seq == "9")
            {
                seq = "0" + seq;
            }
            tstrseq = str + "-" + seq;            
            opengrid();
        }

        private void opengrid()
        {
            gridpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(gridpage_DocumentCompleted);
            gridpage.AllowNavigation = true;
            string wp = "http://www.bldgportal.com/DeviationLog/Application/RecordList_Grid.aspx?Find=";
            url = string.Format("{0}{1}", wp, str);           
            gridpage.Navigate(url, false);            
        }        

        void gridpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {     
            HtmlElement table = gridpage.Document.GetElementById("dataGrid");
            if (table == null)
            {
                observationlog.newpage(tridenttable, tr);
            }
            rows = table.Document.GetElementsByTagName("tr");
            rowcount = rows.Count;

            for (int r = 1; r < rowcount; r++)
            {
                row = rows[r];
                cells = row.Document.GetElementsByTagName("td");                
                bpstoreseq = cells[2];
                bpstrseq = bpstoreseq.InnerHtml;

                if (bpstrseq == tstrseq)
                {
                    cell = cells[5];
                    string bpopen = cell.InnerHtml;
                    trow["open"] = bpopen;
                    gridpage.Dispose();
                    break;
                }
            }
            gridpage.Dispose();
            tridenttable.Dispose();
            trow = null;
            rows = null;
            bprow = null;
            bpstrseq = null;
            row = null;
            storeseq = null;
            cells = null;
            cell = null;           
            table = null;            
            this.Close();
        }        
    }
}
